//
//  WBHomeViewController.swift
//  微博
//
//  Created by 康智桐 on 2018/3/27.
//  Copyright © 2018年 Kk. All rights reserved.
//

import UIKit

//原创微博的可重用cellid
private  let originalCellId = "originalCellId"
//被转发微博的可重用cellid
private let retweetedCellId = "retweetedCellId"

class WBHomeViewController: WBBaseViewController {

    
    //列表视图模型
    lazy var listViewModel = WBStatusListViewModel()
    
    
    /// 加载数据
    override func loadData() {
        
        print("准备刷新")
        // Xcode 8.0 的刷新控件，beginRefreshing 方法，什么都不显示！
        refreshControl?.beginRefreshing()
        
        // 模拟演示加载数据
        
       DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
            self.listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess, shouldRefresh) in
                
                print("加载数据结束")
                // 结束刷新控件
                self.refreshControl?.endRefreshing()
                // 恢复上拉刷新标记
                self.isPullup = false
                
                // 刷新表格
                if shouldRefresh {
                    self.tableView?.reloadData()
                }
            }
        }
    }
    @objc  func showFriends(){
        print(#function)
        let vc = WBDemoViewController()
        //push动作时隐藏底部tabbar的Item
        //vc.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(vc, animated: true)
    }

}

extension WBHomeViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        //取视图模型 根据视图模型判断可重用cell
        let vm = listViewModel.statusList[indexPath.row]
        
        let cellId = (vm.status.retweeted_status != nil ) ? retweetedCellId : originalCellId
        
        //取cell
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! WBStatusCell
        //设置cell
        cell.viewModel = vm
        
       //设置代理
        //如果用block需要在数据源中 给每一个cell设置bolck
        
        cell.delegate = self
        
        
        //返回cell
        return cell
        
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        //根据indexPath获取视图模型
        let vm = listViewModel.statusList[indexPath.row]
        
        //返货计算好的行高
        return  vm.rowHeight
        
        
    }
    
    
}
// MARK: - WBStatusCellDelegate
extension WBHomeViewController: WBStatusCellDelegate {
    
    func statusCellDidSelectedURLString(cell: WBStatusCell, urlString: String) {
        
        let vc = WBWebViewController()
        
        vc.urlString = urlString
        
        navigationController?.pushViewController(vc, animated: true)
        
    }
    
}

extension WBHomeViewController{
    
    override func setupTableView() {
        super.setupTableView()
        //设置导航栏按钮
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        
        //注册原型cell
       
       
        tableView?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: originalCellId)
        tableView?.register(UINib(nibName: "WBStatusRetweetedCell", bundle: nil), forCellReuseIdentifier: retweetedCellId)
        
        //设置行高
        //tableView?.rowHeight = UITableViewAutomaticDimension
        tableView?.estimatedRowHeight = 300
        
        //取消分割线
        tableView?.separatorStyle = .none
        
        setupNavTitle()
    }
    //设置导航栏标题
        private func setupNavTitle(){
    
            let title = WBNetworkManager.shared.userAccount.screen_name
            
            let button = WBTitleButton(title: title)
            
            navItem.titleView = button
            
            button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
            
    }
    
    @objc func clickTitleButton(btn : UIButton){
        
        //选中状态
        btn.isSelected = !btn.isSelected
        
    }
    
    
}
